Create a web page presentation using R Markdown that features a plot created with Plotly. Host your webpage on either GitHub Pages, RPubs, or NeoCities. Your webpage must contain the date that you created the document, and it must contain a plot created with Plotly. We would love to see you show off your creativity! Review criteria
The rubric contains the following two questions:
Does the web page feature a date and is this date less than two months before the date that you’re grading this assignment?
Is the web page a presentation and does it feature an interactive plot that appears to have been created with Plotly?
library(plotly)
library(reshape2)
library(tidyverse)
library(tidymodels)
library(pastecs)
data(tips)
Here we will plot prediction of tips based on total bill paied using the built-in tips dataset in R.
# set x as total bill and y as tip amount
y <- tips$tip
X <- tips$total_bill
# Split the data
set.seed(123)
tips_split <- initial_split(tips)
tips_training <- tips_split %>%
training()
tips_test <- tips_split %>%
testing()
# fit the training the data to an OLS model
lm_model <- linear_reg() %>%
set_engine('lm') %>%
set_mode('regression') %>%
fit(tip ~ total_bill, data = tips_training)
# using the model to predict a randomly generated column of total bill
x_range <- seq(min(X), max(X), length.out = 100)
x_range <- matrix(x_range, nrow=100, ncol=1)
xdf <- data.frame(x_range)
colnames(xdf) <- c('total_bill')
ydf <- lm_model %>%
predict(xdf)
## generate the prediction matrix
colnames(ydf) <- c('tip')
xy <- data.frame(xdf, ydf)
# Plotting
fig <- plot_ly(data = tips_training, x = ~total_bill, y = ~tip, type = 'scatter', name = 'train', mode = 'markers', alpha = 0.65) %>%
add_trace(data = tips_test, x = ~total_bill, y = ~tip, type = 'scatter', name = 'test', mode = 'markers', alpha = 0.65 ) %>%
add_trace(data = xy, x = ~total_bill, y = ~tip, name = 'prediction', mode = 'lines', alpha = 1)
fig